package com.ylxn.five;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.ylxn.utils.RabbitMqUtils;

/**
 * @author cenghaile
 * @version 1.0
 * 2021/7/1 5:40 下午
 **/
public class ReceiveLogs02 {
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        /**
         * 生成一个临时队列，队列名称随机
         * 当消费者断开与队列的连接的时候，队列自动删除
         */
        String queue = channel.queueDeclare().getQueue();
        /**
         * 绑定交换机
         */
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("222等待接收消息。。。");
        DeliverCallback deliverCallback = (consumerTag,message)->{
            System.out.println("222接收消息成功" + new String(message.getBody(),"UTF-8"));
        };
        CancelCallback cancelCallback = (consumerTag)->{
            System.out.println("222接收消息失败" + consumerTag);
        };
        channel.basicConsume(queue,true,deliverCallback,cancelCallback);
    }
}
